package com.aidex.biz.controller;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.domain.SearchInfo;
import com.aidex.biz.service.CustomerService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 左心房客户推送信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/biz/bizCustomer")
public class CustomerController extends BaseController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询左心房客户推送信息列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Customer customer, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customer.setCompanyId(null);
        } else {
            customer.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customer.setPage(new PageDomain(request, response));
        return R.data(customerService.findPage(customer));
    }

    /**
     * 获取左心房客户推送信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:query')")
    @GetMapping(value = "/{id}")
    public R<Customer> detail(@PathVariable("id") String id) {
        return R.data(customerService.get(id));
    }

    @GetMapping(value = "/manualShare")
    public R manualShare(Customer customer) {
        Customer customerOne=customerService.get(customer.getId());
        SysUser sysUser=iSysUserService.get(customer.getUserId());
        if(null==customerOne||null==sysUser){
            return R.fail("业务对象为空！");
        }
        return customerService.shareCustomer(customerOne,sysUser)>=1?R.success():R.fail("账户余额不足，请充值");
    }


    /**
     * 新增左心房客户推送信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:add')")
    @Log(title = "左心房客户推送信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Customer customer) {
        return R.status(customerService.save(customer));
    }

    /**
     * 修改左心房客户推送信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:edit')")
    // @Log(title = "左心房客户推送信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Customer customer) {
        if (!StringUtils.isNull(customer.getIsDail())&&1L==customer.getIsDail()) {
            customer.setDailTime(new Date());
        }
        return R.status(customerService.save(customer));
    }


    /**
     * 删除左心房客户推送信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:remove')")

    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerService.deleteCustomerByIds(ids));
    }

    /**
     * 获取最大编号
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerService.findMaxSort(new Customer()));
    }

    /**
     * 导出左心房客户推送信息列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:bizCustomer:export')")
    //  @Log(title = "左心房客户导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Customer customer) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customer.setCompanyId(null);
        } else {
            customer.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<Customer> list = customerService.findList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "德驰客户推送信息数据");
    }

    @Log(title = "客户信息导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        List<Customer> customerList = util.importExcel(file.getInputStream());
        String message = customerService.importData(customerList);
        return AjaxResult.success(message);
    }


}
